/** m2settings is a GUI to configure Maven 2 settings.xml
 *
 *  Copyright (C) 2007 Regis Decamps <decamps@users.sf.net>
 *
 *  This file is part of m2settings.
 *
 *  m2settings is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  Foobar is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Foobar; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *  or read it online http://www.gnu.org/licenses/gpl.txt
 *
 **/
/*
 * MyRepository.java
 *
 * Created on May 1, 2007, 10:15 PM
 *
 *
 */

package info.decamps.m2settings.beans;

import info.decamps.m2settings.model.PluginRepository;
import info.decamps.m2settings.model.Releases;
import info.decamps.m2settings.model.Repository;
import info.decamps.m2settings.model.RepositoryPolicyType;
import java.text.ParseException;
import org.apache.commons.lang.StringUtils;

/**
 * This class encapsultates a repository or a plugin repository.
 * @author regis
 */
public class MyRepository {
    public final static String[] UPDATE_POLICIES={"daily", "always", "interval:", "never" };
    public final static String[] LAYOUT={"default","legacy"};
    public final static String[] CHECKSUM_POLICY={"fail","warn"};
    private RepositoryPolicyType repository;
    
    /** Creates a new instance of MyRepository */
    public MyRepository(Repository repository) {
        if (repository==null) {
            repository=new Repository();
        }
        this.repository=repository;
    }
    
    public MyRepository(PluginRepository repository) {
        if (repository==null) {
            repository=new PluginRepository();
        }
        this.repository=repository;
    }
    
    public String getId() {
        return repository.getId();
    }
    
    public String getLayout() {
        return repository.getLayout();
    }
    
    public String getName() {
        return repository.getName();
    }
    
    public String getUrl() {
        return repository.getUrl();
    }
    
    private Releases getReleases() {
        Releases r=repository.getReleases();
        if (r==null) r=new Releases();
        return r;
    }
    public boolean getReleasesEnabled() {
        return getReleases().getEnabled();
    }
    
    public String getReleasesUpdatePolicy() {
        String s=getReleases().getUpdatePolicy();
        if (s==null) s=UPDATE_POLICIES[0];
        return s.split(":")[0];
    }
    
    public int getReleasesUpdateInterval() {
        String s=getReleases().getUpdatePolicy();
        try {
            String val=s.split(":")[1];
            return Integer.parseInt(val);
        } catch (ArrayIndexOutOfBoundsException e) {
            //e.printStackTrace();
            //e no ":" in string
            return 0;
        } catch (NumberFormatException e) {
            //e.printStackTrace();
            //interval:XX where XX is not a number
            return 0;
        } catch (NullPointerException e) {
            //no update policy defined yet
            return 0;
        }
    }
    
    public String getReleasesChecksumPolicy() {
        return getReleases().getChecksumPolicy();
    }
    
    public RepositoryPolicyType getRepository() {
        return repository;
    }
    
    public void setId(String id) {
        repository.setId(id);
    }
    public void setLayout(String layout) {
        if (StringUtils.isBlank(layout)) {
            layout=null;
        }
        repository.setLayout(layout);
    }
    public void setName(String name) {
        if (StringUtils.isBlank(name))
            name=null;
        repository.setName(name);
    }
    public void setReleasesChecksumPolicy(String checksumPolicy) {
        if (StringUtils.isBlank(checksumPolicy))
            checksumPolicy=null;
        getReleases().setChecksumPolicy(checksumPolicy);
    }
    
    public void setReleasesEnabled(boolean enabled) {
        getReleases().setEnabled(enabled);
    }
    
    public void setReleasesUpdate(String policy, int interval) {
        String u;
        if (UPDATE_POLICIES[2].equals(policy)) {
            u=policy+":"+interval;
        } else {
            u=policy;
        }
        if (StringUtils.isBlank(u))
            u=null;
        getReleases().setUpdatePolicy(u);
    }
    
    public void setUrl(String url) {
        if (StringUtils.isBlank(url))
            url=null;
        repository.setUrl(url);
    }
}
